﻿using System;
using System.Data;
using System.Collections.Generic;
using Manager.Tools;
using Manager.Model.Admin;
namespace Manager.BLL.Admin
{
    /// <summary>
    /// M_RoleModule
    /// </summary>
    public partial class M_RoleModuleBll
    {
        private readonly Manager.DAL.Admin.M_RoleModuleDal dal = new Manager.DAL.Admin.M_RoleModuleDal();
        public M_RoleModuleBll()
        { }
        /// <summary>
        /// 检查模块角色权限
        /// </summary>
        /// <param name="RoleID"></param>
        /// <param name="ModuleID"></param>
        /// <param name="ope"></param>
        /// <returns></returns>
        public bool CheckPermission(int RoleID, int ModuleID, string ope)
        {
            Manager.BLL.Admin.M_RoleModuleBll bllRM = new Manager.BLL.Admin.M_RoleModuleBll();
            System.Data.DataSet ds = bllRM.GetList("RoleID=" + RoleID + " and ModuleID=" + ModuleID);
            System.Data.DataTable dt = ds.Tables[0];
            if (dt.Rows.Count > 0)
            {
                string cks = dt.Rows[0]["Permission"].ToString();
                if (cks.IndexOf(ope) > -1)
                    return true;
                else
                    return false;
            }
            else
            {
                return false;
            }
        }

        #region  Method
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int RMID)
        {
            return dal.Exists(RMID);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Manager.Model.Admin.M_RoleModule model)
        {
            return dal.Add(model);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Manager.Model.Admin.M_RoleModule model)
        {
            return dal.Update(model);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int RMID)
        {
            return dal.Delete(RMID);
        }
        /// <summary>
        /// 删除数据根据roleid
        /// </summary>
        /// <param name="RoleID"></param>
        /// <returns></returns>
        public bool DeleteByRoleID(int RoleID)
        {
            return dal.DeleteByRoleID(RoleID);
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool DeleteList(string RMIDlist)
        {
            return dal.DeleteList(RMIDlist);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Manager.Model.Admin.M_RoleModule GetModel(int RMID)
        {

            return dal.GetModel(RMID);
        }

        /// <summary>
        /// 得到一个对象实体，从缓存中
        /// </summary>
        public Manager.Model.Admin.M_RoleModule GetModelByCache(int RMID)
        {

            string CacheKey = "M_RoleModuleModel-" + RMID;
            object objModel = Manager.Tools.DataCache.GetCache(CacheKey);
            if (objModel == null)
            {
                try
                {
                    objModel = dal.GetModel(RMID);
                    if (objModel != null)
                    {
                        int ModelCache = Manager.Tools.ConfigHelper.GetConfigInt("ModelCache");
                        Manager.Tools.DataCache.SetCache(CacheKey, objModel, DateTime.Now.AddMinutes(ModelCache), TimeSpan.Zero);
                    }
                }
                catch { }
            }
            return (Manager.Model.Admin.M_RoleModule)objModel;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            return dal.GetList(strWhere);
        }
        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            return dal.GetList(Top, strWhere, filedOrder);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<Manager.Model.Admin.M_RoleModule> GetModelList(string strWhere)
        {
            DataSet ds = dal.GetList(strWhere);
            return DataTableToList(ds.Tables[0]);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<Manager.Model.Admin.M_RoleModule> DataTableToList(DataTable dt)
        {
            List<Manager.Model.Admin.M_RoleModule> modelList = new List<Manager.Model.Admin.M_RoleModule>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                Manager.Model.Admin.M_RoleModule model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = new Manager.Model.Admin.M_RoleModule();
                    if (dt.Rows[n]["RMID"] != null && dt.Rows[n]["RMID"].ToString() != "")
                    {
                        model.RMID = int.Parse(dt.Rows[n]["RMID"].ToString());
                    }
                    if (dt.Rows[n]["RoleID"] != null && dt.Rows[n]["RoleID"].ToString() != "")
                    {
                        model.RoleID = int.Parse(dt.Rows[n]["RoleID"].ToString());
                    }
                    if (dt.Rows[n]["ModuleID"] != null && dt.Rows[n]["ModuleID"].ToString() != "")
                    {
                        model.ModuleID = int.Parse(dt.Rows[n]["ModuleID"].ToString());
                    }
                    if (dt.Rows[n]["Permission"] != null && dt.Rows[n]["Permission"].ToString() != "")
                    {
                        model.Permission = dt.Rows[n]["Permission"].ToString();
                    }
                    modelList.Add(model);
                }
            }
            return modelList;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetAllList()
        {
            return GetList("");
        }

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            return dal.GetRecordCount(strWhere);
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            return dal.GetListByPage(strWhere, orderby, startIndex, endIndex);
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        //public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        //{
        //return dal.GetList(PageSize,PageIndex,strWhere);
        //}

        #endregion  Method
    }
}

